<?php
defined('IN_XLP') or exit('Access Denied!');

class wmadminModel extends model {

    function __construct() {
        parent::__construct();
        $this->dbTable = 'wm_admin';
    }

    public function getUserInfoById($id) {
        return $this->where(array('id' => $id))->find();
    }

    /*
     * 管理员登陆设置
     * @param $userInfo array 管理员信息数组
     * @example array('id'=>1,'pwd'=>XXXXXX)
     * @param $remember int cookie有效时间
     * @param $saveLogin bool 是否更新登录信息
     * @return bool
     */
    public function setUserLogin($userInfo = array(), $remember = 0, $saveLogin = true) {
        if ($saveLogin) {
            $this->update("`login_count`=`login_count`+1,
                           `last_login_ip`='" .getUserIp(). "',
                           `time_last_login`='" .TIME. "'",
                           array('id' => $userInfo['id']));
        }
        $saltkey = getRandStr(8);
        $auth = setEnocde($userInfo['id'] ."\t". $userInfo['pwd'], wmadmin::getAuthKey($saltkey));
        myCookie('wms_saltkey', $saltkey, $remember);
        myCookie('wms_auth', $auth, $remember);
        return true;
    }


    /*
     * 生成用户密码
     * @param $pwd string 原始密码
     * @return string 加密处理后的密码
     */
    public function setUserPassword($pwd = '') {
        return md5(crypt($pwd, substr($pwd, 0, 2)));
    }

}
